package com.ibiaoke.system.service;

import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.system.dao.FloorItemDao;
import com.ibiaoke.common.exception.ServiceException;
import com.ibiaoke.system.entity.FloorItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/6/16.
 */
@Service("floorItemService")
public class FloorItemServiceImpl implements FloorItemService{

    @Autowired
    private FloorItemDao floorItemDao;

    @Override
    public int saveFloorItem(FloorItem floorItem) {
        AssertUtils.notNull(floorItem.getFloorId(),"楼层Id不能为空");
        if (floorItem.getType()==1) {
            AssertUtils.notNull(floorItem.getItemObject(),"商品Id不能为空");
        }
        if (floorItem.getType()==2) {
            AssertUtils.notBlank(floorItem.getItemUrl(),"链接不能为空");
        }
        if (floorItem.getType()==3) {
            AssertUtils.notNull(floorItem.getItemObject(),"分类Id不能为空");
        }

        floorItem.setDelStatus(0);
        floorItem.setCreateTime(new Date());
        floorItem.setUpdateTime(new Date());
        return floorItemDao.insert(floorItem);
    }

    @Override
    public int updateByFloorItemId(FloorItem floorItem) {
        if (floorItem.getFloorItemId() == null) {
            throw new ServiceException("FloorItem编号不能为空");
        }
        floorItem.setUpdateTime(new Date());
        return floorItemDao.updateById(floorItem);
    }

    @Override
    public int deleteByFloorItemId(Integer floorItemId) {
        if (floorItemId == null) {
            throw new ServiceException("FloorItem编号不能为空");
        }
        FloorItem floorItem = new FloorItem();
        floorItem.setFloorItemId(floorItemId);
        floorItem.setDelStatus(1);
        return floorItemDao.updateByPrimaryKeySelective(floorItem);
    }

    @Override
    public List<FloorItem> listFloorItems(Integer floorId,Integer shopId) {
        List<FloorItem> floorItemList = floorItemDao.listFloorItems(floorId,shopId);
        return floorItemList;
    }
}
